package com.cutebaby.admin.com.mybatis.util;

import com.cutebaby.admin.com.mybatis.dao.UserMapper;
import com.cutebaby.admin.com.mybatis.entity.A;
import com.cutebaby.admin.com.mybatis.entity.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * @author: xialegemo
 * @description: 自定义鲁班Bean工厂后置处理器
 * @date： 2020/10/26 22:55
 **/
@Component
public class LuBanBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {


//        BeanDefinition user = beanFactory.getBeanDefinition("user");//直接通过BeanFactory获取到BeanDefinition信息

        // 使用GenericBeanDefinition更加好用  实际上原理是一致的
        GenericBeanDefinition beanDefinition = (GenericBeanDefinition) beanFactory.getBeanDefinition("user");
        // 通过BeanDefinition可以对bean信息进行修改
        beanDefinition.setBeanClass(A.class);
        beanDefinition.setBeanClass(User.class);
//        beanDefinition.setBeanClassName("abc");

        User user = (User)beanFactory.getBean("user");
        System.out.println("我通过BeanFactory直接获取到了Bean：内存地址为：" + user);

        // spring整合mybatis
        // 方法1：直接使用BeanFactory的注册方法，把mybatis的代理对象注册到bean工厂中供spring管理
        beanFactory.registerSingleton("userMapper", new User());

        // 方法2  @Bean 注解


    }
}
